package com.atguigu.lease.app.service.impl;

import com.atguigu.lease.app.mapper.ApartmentInfoMapper;
import com.atguigu.lease.app.mapper.ViewAppointmentMapper;
import com.atguigu.lease.app.service.ViewAppointmentService;
import com.atguigu.lease.common.handler.StgyAppAuthInterceptor;
import com.atguigu.lease.model.bean.ViewAppointment;
import com.atguigu.lease.model.vo.app.ApartmentItemVo;
import com.atguigu.lease.model.vo.app.AppointmentDetailVo;
import com.atguigu.lease.model.vo.app.AppointmentItemVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author YouKinn
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2025-06-20 20:18:17
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public List<AppointmentItemVo> listItem() {
        return baseMapper.selectListItem(StgyAppAuthInterceptor.APP_USER_THREAD_LOCAL.get().getId());
    }

    @Override
    public AppointmentDetailVo getDetailById(Long id) {
        //预约数据
        //        ViewAppointment = baseMapper.selectById(id);
        ViewAppointment viewAppointment = baseMapper.selectOne(Wrappers.lambdaQuery(ViewAppointment.class)
                .eq(ViewAppointment::getId, id)
                .eq(ViewAppointment::getUserId, StgyAppAuthInterceptor.APP_USER_THREAD_LOCAL.get().getId()));

        //预约公寓的 ApartmentItemVo
        ApartmentItemVo apartmentItemVo = apartmentInfoMapper.selectApartmentItemVo(viewAppointment.getApartmentId().longValue());
        //将上面查询到的数据组装为前端需要的对象
        AppointmentDetailVo vo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment, vo);
        vo.setApartmentItemVo(apartmentItemVo);
        return vo;
    }
}




